<?php
/**
 * S3云存储插件 - 日志查看页面
 * 
 * @package CommonCloudStorage
 * @author xiagg
 * @version 1.0
 */

require '../../../zb_system/function/c_system_base.php';
require '../../../zb_system/function/c_system_admin.php';

$zbp->Load();

// 权限检查
if (!$zbp->CheckRights('root')) {
    $zbp->ShowError(6);
    die();
}

$logFile = $zbp->path . 'zb_users/plugin/CommonCloudStorage/log.txt';
$logs = array();

if (file_exists($logFile)) {
    $content = file_get_contents($logFile);
    $lines = explode("\n", $content);
    $lines = array_reverse(array_filter($lines)); // 倒序显示，过滤空行
    $logs = array_slice($lines, 0, 100); // 只显示最近100条
}

// 清空日志
if (isset($_POST['clear_log'])) {
    file_put_contents($logFile, '');
    header('Location: log.php');
    exit;
}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>S3云存储插件 - 日志查看</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }
        .container { max-width: 1200px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .header { text-align: center; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 2px solid #007cba; }
        .toolbar { margin-bottom: 20px; text-align: right; }
        .btn { background-color: #007cba; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; }
        .btn:hover { background-color: #005a87; }
        .btn-danger { background-color: #dc3545; }
        .btn-danger:hover { background-color: #c82333; }
        .log-container { background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 15px; max-height: 600px; overflow-y: auto; }
        .log-entry { margin-bottom: 8px; padding: 5px; font-family: monospace; font-size: 13px; }
        .log-entry:nth-child(even) { background-color: #ffffff; }
        .log-success { color: #28a745; }
        .log-error { color: #dc3545; }
        .log-info { color: #007cba; }
        .empty-log { text-align: center; color: #6c757d; padding: 40px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>S3云存储插件 - 操作日志</h1>
            <p><a href="admin.php" class="btn">返回管理界面</a></p>
        </div>

        <div class="toolbar">
            <form method="post" style="display: inline;" onsubmit="return confirm('确定要清空所有日志吗？');">
                <button type="submit" name="clear_log" class="btn btn-danger">清空日志</button>
            </form>
        </div>

        <div class="log-container">
            <?php if (empty($logs)): ?>
                <div class="empty-log">暂无日志记录</div>
            <?php else: ?>
                <?php foreach ($logs as $log): ?>
                    <div class="log-entry <?php 
                        if (strpos($log, '成功') !== false) echo 'log-success';
                        elseif (strpos($log, '失败') !== false || strpos($log, '异常') !== false || strpos($log, '错误') !== false) echo 'log-error';
                        else echo 'log-info';
                    ?>"><?php echo htmlspecialchars($log); ?></div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <div style="margin-top: 20px; text-align: center; color: #6c757d; font-size: 12px;">
            显示最近100条日志记录，按时间倒序排列
        </div>
    </div>
</body>
</html>